package com.xinghou.platform.service.main.impl;

import com.xinghou.platform.dao.main.UserRelationFromDAO;
import com.xinghou.platform.dao.main.UserRelationToDAO;
import com.xinghou.platform.model.main.UserRelationFrom;
import com.xinghou.platform.model.main.UserRelationTo;
import com.xinghou.platform.service.main.UserRelationService;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("userRelationService")
public class UserRelationServiceImpl implements UserRelationService{
	
	@Resource
	private UserRelationFromDAO userRelationFromDAO;
	
	@Resource
	private UserRelationToDAO userRelationToDAO;

	@Override
	public void saveUserRelation(UserRelationFrom userRelationFrom,UserRelationTo userRelationTo) throws DataAccessException {
		userRelationFromDAO.saveUserRelationFrom(userRelationFrom);
		userRelationToDAO.saveUserRelationTo(userRelationTo);
	}

	@Override
	public void updateUserRelation(UserRelationFrom userRelationFrom,UserRelationTo userRelationTo) throws DataAccessException {
		userRelationFromDAO.updateUserRelationFrom(userRelationFrom);
		userRelationToDAO.updateUserRelationTo(userRelationTo);
	}
	
	public int getUserRelation(UserRelationFrom userRelationFrom,UserRelationTo userRelationTo) throws DataAccessException {
		UserRelationFrom userRelationFromCheck= userRelationFromDAO.getUserRelationFromByUserId(userRelationFrom.getFromuserid(), userRelationFrom.getTouserid());
		UserRelationFrom userRelationFromCheckTo= userRelationFromDAO.getUserRelationFromByUserId(userRelationFrom.getTouserid(),userRelationFrom.getFromuserid());
		
		if(userRelationFromCheck!=null&&userRelationFromCheckTo!=null){
			if(userRelationFromCheck.getIsliked()==1&&userRelationFromCheckTo.getIsliked()==1){
				return 2;
			}else if(userRelationFromCheck.getIsliked()==1&&userRelationFromCheckTo.getIsliked()==0){
				return 1;
			}else{
				return 3;
			}
		}else if(userRelationFromCheck!=null&&userRelationFromCheckTo==null){
			if(userRelationFromCheck.getIsliked()==1){
				return 1;
			}else{
				return 3;
			}
		}
		return 0;
	}
	
	public int getUserShields(UserRelationFrom userRelationFrom,UserRelationTo userRelationTo) throws DataAccessException {
		UserRelationFrom userRelationFromCheck= userRelationFromDAO.getUserRelationFromByUserId(userRelationFrom.getFromuserid(), userRelationFrom.getTouserid());
		UserRelationFrom userRelationFromCheckTo= userRelationFromDAO.getUserRelationFromByUserId(userRelationFrom.getTouserid(),userRelationFrom.getFromuserid());
		
		if(userRelationFromCheck!=null&&userRelationFromCheckTo!=null){
			if(userRelationFromCheck.getIsblocked()==1&&userRelationFromCheckTo.getIsblocked()==1){
				return 2;
			}else if(userRelationFromCheck.getIsblocked()==1&&userRelationFromCheckTo.getIsblocked()==0){
				return 1;
			}else if(userRelationFromCheck.getIsblocked()==0&&userRelationFromCheckTo.getIsblocked()==1){
				return 3;
			}
		}else if(userRelationFromCheck!=null&&userRelationFromCheckTo==null){
			if(userRelationFromCheck.getIsblocked()==1){
				return 1;
			}
		}else if(userRelationFromCheck==null&&userRelationFromCheckTo!=null){
			if(userRelationFromCheckTo.getIsblocked()==1){
				return 3;
			}
		}
		return 0;
	}

}
